﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Modles.DTO;
using Modles.Models;
using static BilbilDAL.VideoLikeDAL;
using Microsoft.EntityFrameworkCore;

namespace BilbilDAL
{
    public class VideoLikeDAL
    {

        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="userintfo"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public bool AddVideoLike(int userintfo, int videoInfoId)
        {
            
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                VideoLike videoLike = con.VideoLikes.FirstOrDefault(x => x.VideoInfoId == videoInfoId && x.UserInfoId == userintfo);
                if (videoLike == null)
                {
                    con.VideoLikes.Add(new VideoLike() { UserInfoId = userintfo, VideoInfoId = videoInfoId});
                } else
                {
                    return false;
                }
                return con.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 取赞
        /// </summary>
        /// <param name="userintfo"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public bool DeleteVideoLike(int userintfo, int videoInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                VideoLike videoLike = con.VideoLikes.FirstOrDefault(x => x.VideoInfoId == videoInfoId && x.UserInfoId == userintfo);
                if (videoLike != null)
                {
                    con.VideoLikes.Remove(videoLike);
                }
                else
                {
                    return false;
                }
                return con.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 获取用户是否点赞以及视频点赞量
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <param name="LikeCount"></param>
        /// <returns></returns>
        public bool GetVideoLike(int userInfoId ,int videoInfoId, out int LikeCount)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                LikeCount = con.VideoLikes.Where(x => x.VideoInfoId == videoInfoId).Count();
                return con.VideoLikes.Where(x => x.UserInfoId == userInfoId && x.VideoInfoId == videoInfoId).Any();
            }
        }
    }
}
